package com.cus.tom.transfer.frag;

import static android.app.Activity.RESULT_OK;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.cus.tom.transfer.ac.ChooseAct;
import com.cus.tom.transfer.ac.MainActivity;
import com.cus.tom.transfer.ac.ScanAct;
import com.cus.tom.transfer.ac.SendAct;
import com.cus.tom.transfer.bean.Const;
import com.cus.tom.transfer.databinding.FragChooseBinding;
import com.cus.tom.transfer.utils.Read;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;

import org.json.JSONObject;

public class ChangeFragment extends BaseFragment {

    FragChooseBinding binding;
    final Runnable runnable = new Runnable() {
        @Override
        public void run() {
            Intent i = new Intent(getActivity(), ScanAct.class);
            startActivityForResult(i, 1000);
        }
    };

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = FragChooseBinding.inflate(inflater, container, false);
        binding.old.setOnClickListener(v -> {
            Intent i = new Intent(getActivity(), ChooseAct.class);
            startActivity(i);
        });
        binding.now.setOnClickListener(v -> {
            Read.toRead(getActivity(), "需要读取存储文件权限以便存储接收的文件", new Runnable() {
                @Override
                public void run() {
                    if (XXPermissions.isGranted(getActivity(), Permission.MANAGE_EXTERNAL_STORAGE)) {
                        if (!XXPermissions.isGranted(getActivity(), Permission.CAMERA, Permission.ACCESS_COARSE_LOCATION, Permission.ACCESS_FINE_LOCATION)) {
                            TipDlg dlg = new TipDlg().setMsg("为了扫码和旧机建立通信，需要您授予摄像头和定位权限");
                            dlg.setOnDismissListener(new BaseDialogFragment.OnDismissListener() {
                                @Override
                                public void onDismiss(boolean isOk, Object value) {
                                    if (isOk) {
                                        Read.toSetting(getActivity(), runnable, false, Permission.CAMERA, Permission.ACCESS_COARSE_LOCATION, Permission.ACCESS_FINE_LOCATION);
                                    }
                                }
                            });
                            dlg.show(getChildFragmentManager(), "");
                        } else {
                            runnable.run();
                        }
                    }
                }
            });
        });
        binding.set.setOnClickListener(v -> {
            ((MainActivity) getActivity()).toMe();
        });
        return binding.getRoot();
    }

    @Override
    protected boolean paddingTop() {
        return true;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1000 && resultCode == RESULT_OK) {
            String s = data.getStringExtra("result");
            try {
                JSONObject obj = new JSONObject(s);
                String id = obj.getString("id");
                String me = Read.getWifiId(getActivity());
                if (!me.equals(id)) {
                    new TipDlg().setMsg("新机和旧机不在同一WIFI下").setOk("确定").show(getChildFragmentManager(), "");
                    return;
                }
                String host = obj.getString("address");
                int port = obj.getInt("port");
                Const.TCP_PORT = port;
                Intent i = new Intent(getActivity(), SendAct.class);
                i.putExtra("type", 1);
                i.putExtra("host", host);
                startActivity(i);
            } catch (Throwable e) {
                e.printStackTrace();
            }
        }
    }
}
